package kt.com.bilin.coin.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import androidx.fragment.app.Fragment
import com.bilin.coin.R
import kotlinx.android.synthetic.main.dialog_alert.*
import kt.com.bilin.coin.common.getString
import kt.com.bilin.coin.common.gone

private typealias callback = (() -> Unit)?
class AlertDialog(context: Context) :Dialog(context, R.style.dimLoadingdialog),View.OnClickListener{
    init { setContentView(R.layout.dialog_alert) }

    override fun onCreate(savedInstanceState: Bundle?) {
        (titleStr ?: R.string.dialog_title.getString()).let { title.text = it }
        messageStr?.let { message.text = it }
        if (positiveButtonCallback!= null && positiveButtonName.isNullOrEmpty()) positiveButtonName = R.string.button_confirm.getString()
        if (!positiveButtonName.isNullOrEmpty()){
            positiveButton.apply {
                text = positiveButtonName
                setOnClickListener(this@AlertDialog)
            }
        }else positiveButton.gone()
        if (negativeButtonCallback!= null && negativeButtonName.isNullOrEmpty()) negativeButtonName = R.string.button_cancel.getString()
        if (!negativeButtonName.isNullOrEmpty()){
            negativeButton.apply {
                text = negativeButtonName
                setOnClickListener(this@AlertDialog)
            }
        }else negativeButton.gone()
        setCancelable(mCancelable)
        window?.attributes?.run {width = (context.resources.getDisplayMetrics().widthPixels * 0.85).toInt() }
        message.movementMethod = ScrollingMovementMethod.getInstance();
    }

    override fun onClick(v: View?) {
        when(v){
            positiveButton->v.let { if (positiveDismissAfterClick) dismiss();positiveButtonCallback?.invoke() }
            negativeButton->v.let { if (negativeDismissAfterClick) dismiss();negativeButtonCallback?.invoke() }
        }
    }
    private var titleStr:CharSequence? = null
    private var messageStr: CharSequence? = null
    private var positiveButtonName:CharSequence?=null
    private var positiveButtonCallback: callback = null
    private var positiveDismissAfterClick:Boolean = true
    private var negativeButtonName:CharSequence?=null
    private var negativeButtonCallback: callback = null
    private var negativeDismissAfterClick:Boolean = true
    var mCancelable: Boolean = true


    fun title(titleRes: Int = 0, title: CharSequence = ""){
        titleStr = if (titleRes != 0) titleRes.getString() else title
    }
    fun message(messageRes: Int = 0, message: CharSequence = ""){
        messageStr = if (messageRes != 0) messageRes.getString() else message
    }
    fun positiveButton(nameRes: Int = 0, name: CharSequence = "", dismissAfterClick: Boolean = positiveDismissAfterClick, callback: callback = null) {
        positiveButtonName = if (nameRes != 0) nameRes.getString() else name
        positiveDismissAfterClick = dismissAfterClick
        positiveButtonCallback = callback
    }
    fun negativeButton(nameRes: Int = 0, name: CharSequence = "", dismissAfterClick: Boolean = negativeDismissAfterClick, callback: callback = null) {
        negativeButtonName = if (nameRes != 0) nameRes.getString() else name
        negativeDismissAfterClick = dismissAfterClick
        negativeButtonCallback = callback
    }
}

fun Activity?.showAlertDialog(settings: AlertDialog.() -> Unit)  = this?.let { AlertDialog(this).also(settings).show()}
fun Fragment?.showAlertDialog(settings: AlertDialog.() -> Unit)  = this?.let { activity?.let { AlertDialog(it).also(settings).show() }}